import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/game/game_detail/models/model_game_round.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';

// import 'package:flutter_09_game/pages/game/game_detail/widgets/widget_currency_icon.dart';
import 'package:flutter_09_game/utils/utils_other.dart';

import 'package:flutter_09_game/widgets/button/debounce_gesture_detector.dart';
import 'package:flutter_09_game/widgets/empty/empty_no_data.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 投注历史表格组件
/// 用于显示用户的投注记录
class BetHistoryTableWidget extends StatelessWidget {
  final List<ModelGameRoundMyHistory> betHistoryList;
  final VoidCallback? onMoreTap;

  const BetHistoryTableWidget({
    super.key,
    required this.betHistoryList,
    this.onMoreTap,
  });

  @override
  Widget build(BuildContext context) {
    // 最多显示10条数据
    final displayList = betHistoryList.take(10).toList();

    return Container(
      color: AppColor.navBgColor,
      child: Column(
        children: [
          Container(
            height: 40.w,
            padding: EdgeInsets.symmetric(
              horizontal: 12.w,
            ).copyWith(top: 8.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  LocaleKeys.betAmount.tr,
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                    color: AppColor.textPrimary.withOpacity(0.94),
                  ),
                ),
                Visibility(
                  visible: displayList.isNotEmpty,
                  child: DebounceGestureDetector(
                    onTap: onMoreTap,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          LocaleKeys.more.tr,
                          style: TextStyle(
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w400,
                            color: AppColor.primary,
                          ),
                        ),
                        // 4.horizontalSpace,
                        AutoImage(
                          Assets.gameArrowRight,
                          width: 20.w,
                          height: 20.w,
                          color: AppColor.primary,
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
          _buildTableHeader(),
          8.verticalSpaceFromWidth,

          ...displayList.asMap().entries.map(
              (entry) => _buildDataRow(entry.value, isFirst: entry.key == 0)),
          Visibility(
              visible: displayList.isEmpty,
              child: EmptyNoData(
                message: LocaleKeys.noData.tr,
                assets: Assets.commonEmptyNoData,
              ))

          // ...displayList.map((item) => _buildDataRow(item)),
        ],
      ),
    );
  }

  /// 构建表头行
  Widget _buildTableHeader() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      height: 40.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildHeaderCell(
            LocaleKeys.drawBlock.tr,
          ),
          _buildHeaderCell(
            LocaleKeys.currency.tr,
          ),
          _buildHeaderCell(LocaleKeys.stakeAmount.tr),
          _buildHeaderCell(
            LocaleKeys.historyWinLose.tr,
          ),
        ],
      ),
    );
  }

  /// 构建表头单元格
  Widget _buildHeaderCell(String title) {
    return Container(
      alignment: Alignment.centerLeft,
      width: 80.w,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w600,
          color: AppColor.textPrimary.withOpacity(0.3),
        ),
      ),
    );
  }

  /// 构建数据行
  Widget _buildDataRow(ModelGameRoundMyHistory item, {bool isFirst = false}) {
    return Container(
      height: 40.w,
      margin: EdgeInsets.symmetric(vertical: 2.w),
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: isFirst ? AppColor.primary.withOpacity(0.3) : null,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildDataCell(
            _formatRound(item.round),
          ),
          _buildCurrencyCell(
            item.symbol ?? 'USDT',
          ),
          _buildDataCell(
            item.amount.formatWallet(),
          ),
          _buildProfitLossCell(
           item.status == 1 ? 0 : item.isWin == true ? (item.winAmount ?? 0) : -(item.amount ?? 0),
          ),
        ],
      ),
    );
  }

  /// 构建数据单元格
  Widget _buildDataCell(
    String text,
  ) {
    return Container(
      alignment: Alignment.centerLeft,
      width: 80.w,
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      child: Text(
        text,
        style: TextStyle(
          fontSize: 12.sp,
          color: AppColor.textPrimary.withOpacity(0.94),
          fontWeight: FontWeight.w400,
        ),
      ),
    );
  }

  /// 构建币种单元格
  Widget _buildCurrencyCell(
    String symbol,
  ) {
    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 8.w),
      alignment: Alignment.centerLeft,
      width: 80.w,
      // child: CurrencyIconWidget(
      //   symbol: symbol,
      //   iconSize: 24.w,
      //   fontSize: 12.sp,
      // ),
      child: Row(
        children: [
          AutoImage(
            UtilsOther().getAmountTagIcon(symbol),
            width: 24.w,
          ),
          4.horizontalSpace,
          Text(
            symbol,
            style: TextStyle(
                color: AppColor.textPrimary.withOpacity(0.7), fontSize: 14.sp),
          )
        ],
      ),
    );
  }

  /// 构建盈亏单元格
  Widget _buildProfitLossCell(int winAmount) {
    // final isProfit = winAmount > 0;
    final color = winAmount > 0
        ? AppColor.homePrizePoolIndicator
        : winAmount == 0
            ? AppColor.textPrimary.withOpacity(0.7)
            : AppColor.gameResultEven;
    final prefix = winAmount > 0
        ? '+'
        : winAmount == 0
            ? '--'
            : '';

    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 8.w),

      alignment: Alignment.centerLeft,
      width: 80.w,
      child: Text(
        winAmount == 0 ? prefix : '$prefix${winAmount.formatWallet()}',
        style: TextStyle(
          fontSize: 12.sp,
          color: color,
          fontWeight: FontWeight.w900,
        ),
      ),
    );
  }

  /// 格式化区块号
  String _formatRound(int? round) {
    if (round == null) return '--';
    return round.toString();
  }
}
